package com.xuanyang.dormrepair.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xuanyang.dormrepair.config.hook.AdminLogin;
import com.xuanyang.dormrepair.config.hook.AppLogin;
import com.xuanyang.dormrepair.config.hook.OpenApi;
import com.xuanyang.dormrepair.model.entity.FloorRoom;
import com.xuanyang.dormrepair.model.entity.User;
import com.xuanyang.dormrepair.service.FloorRoomService;
import com.xuanyang.dormrepair.service.UserService;
import com.xuanyang.dormrepair.utils.Const;
import com.xuanyang.dormrepair.utils.RestResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @AUTHOR soft
 * @DATE 2019/5/22 21:35
 * @DESCRIBE 用户相关操作的接口
 */
@RestController
public class UserController {

    @Autowired
    private FloorRoomService roomService;

    @Autowired
    private UserService userService;


    private FloorRoom room;

    @ModelAttribute
    public void init(@RequestAttribute(value = Const.USER, required = false) FloorRoom rm) {
        this.room = rm;
    }

    /**
     * 手机端用户登录
     */
    @OpenApi
    @PostMapping("/api/login")
    public RestResponse login(FloorRoom room) {
        FloorRoom login = roomService.login(room);
        if (login != null) {
            String token = login.token();
            login.setToken(token);
            return RestResponse.ok(login);
        }
        return RestResponse.fail("密码错误！");
    }

    /**
     * 手机端用户获取登录者的信息
     */
    @AppLogin
    @PostMapping("/api/info")
    public RestResponse info() {
        if (room != null) {
            return RestResponse.ok(room);
        }
        return RestResponse.fail();
    }

    @AppLogin
    @PostMapping("/api/change")
    public RestResponse change(FloorRoom room) {
        room.setId(this.room.getId())
                .setHouseNo(null)
                .setRoomNo(null);
        if (roomService.update(room)) {
            return RestResponse.ok();
        }
        return RestResponse.fail();
    }


    @AdminLogin
    @PostMapping("/info")
    public RestResponse info(@SessionAttribute(Const.USER) User user) {
        return RestResponse.ok(userService.byId(user.getId()));
    }

    @OpenApi
    @PostMapping("/login")
    public RestResponse login(User user, HttpServletRequest request) {
        User login = userService.login(user);

        if (login != null) {
            request.getSession().setAttribute(Const.USER, login);
            userService.update(new User().setId(login.getId()).setLoginTime(new Date()));

            return RestResponse.ok();
        }
        return RestResponse.fail();
    }

    /**
     * 管理员注册
     * @param user 用户信息
     * @param code 密钥
     */
    @OpenApi
    @PostMapping("/register")
    public RestResponse register(User user, String code) {
        if (StringUtils.isEmpty(code) || !code.equals("98269264")) {
            return RestResponse.fail("密钥错误！");
        }
        user.setStatus(1);
        if (userService.add(user)) {
            return RestResponse.ok();
        }
        return RestResponse.fail();
    }

    /**
     * 管理员信息修改
     */
    @AdminLogin
    @PostMapping("/change")
    public RestResponse change(User user) {
        if (userService.update(user)) {
            return RestResponse.ok();
        }
        return RestResponse.fail();
    }
}
